package com.oldking.utils;

import javafx.stage.FileChooser;

import java.io.*;
import java.text.DecimalFormat;
import java.util.regex.Pattern;

public class CommonFileUtil {
    private static final String VIDEO_FOMAT_LIST_STRING = "Select a File (*.mp4;flv;avi;rm;rmvb;mpeg;3gp;wmv;mpg;mkv;asf;)";
    private static final String VIDEO_FOMAT_LIST = "*.mp4;*.flv;*.avi;*.rm;*.rmvb;*.mpeg;*.3gp;*.wmv;*.mpg;*.mkv;*.asf;";
    private static final String REG = "(mp4|flv|avi|rm|rmvb|wmv|mpeg|3gp|asf|divx|mpg|mpe|mkv|vob)";

    public static File openvidFile() {
        FileChooser fileChooser = new FileChooser();
        FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter(VIDEO_FOMAT_LIST_STRING, VIDEO_FOMAT_LIST);
        fileChooser.getExtensionFilters().add(filter);
        File file = fileChooser.showOpenDialog(null);
        return file;
    }

    public static String formetFileSize(long file) {
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        if (file < 1024) {
            fileSizeString = df.format((double) file) + "B";
        } else if (file < 1048576) {
            fileSizeString = df.format((double) file / 1024) + "K";
        } else if (file < 1073741824) {
            fileSizeString = df.format((double) file / 1048576) + "M";
        } else {
            fileSizeString = df.format((double) file / 1073741824) + "G";
        }
        return fileSizeString;
    }

    public static boolean imgLocation(File file) throws IOException {

        Pattern p = Pattern.compile(REG);
        boolean boo = p.matcher(file.getName()).find();
        return boo;
    }

    public static void copySrt2SrtListFile(File bf, String subtitlePath) {

        File af = new File(subtitlePath);
        FileInputStream is = null;
        FileOutputStream os = null;
        if (!bf.exists()) {
            try {
                bf.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            is = new FileInputStream(af);
            os = new FileOutputStream(bf);
            byte b[] = new byte[1024];
            int len;
            try {
                len = is.read(b);
                while (len != -1) {
                    os.write(b, 0, len);
                    len = is.read(b);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) is.close();
                if (os != null) os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static long getLineNumber(File file) {
        if (file.exists()) {
            try {
                FileReader fileReader = new FileReader(file);
                LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
                lineNumberReader.skip(Long.MAX_VALUE);
                long lines = lineNumberReader.getLineNumber() + 1;
                fileReader.close();
                lineNumberReader.close();
                return lines;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    public static void deleteDirectory(File file) {
        if (file.isFile()) {
            file.delete();//清理文件
        } else {
            File list[] = file.listFiles();
            if (list != null) {
                for (File f : list) {
                    deleteDirectory(f);
                }
                file.delete();//清理目录
            }
        }
    }
}
